﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.BusinessLayer.DauSach;
using WF_QLThuVien.Entities.DauSach;

namespace WF_QLThuVien.PresentationLayer
{
    public partial class FormMain : Form
    {
        CBLL_DauSach bllDauSach = new CBLL_DauSach();
        //List<CDauSach> dataDauSach = null;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            btnTim.Enabled = false;
            this.ControlBox = false;
            //dataDauSach = bllDauSach.LoadData();
            txtTim.Focus();
            dgvKetQua.Visible = false;
        }

        private void btnQuanLy_Click(object sender, EventArgs e)
        {
            
            FormLogIn fli = new FormLogIn();
            fli.open += new FormLogIn.OpenFormQuanLy(fli_open);
            fli.ShowDialog();                 
        }

        void fli_open(bool flag)
        {
            if (flag)
            {
                FormQuanLy fql = new FormQuanLy();
                this.Visible = false;

                fql.thoat += new FormQuanLy.DongForm(fql_thoat);
                fql.ShowDialog();
            }
        }

        void fql_thoat(bool flag)
        {
            if (flag)
            {
                this.Close();
            }
            else
            {
                this.Visible = true;
                //dataDauSach = bllDauSach.LoadData();
                txtTim.Focus();
                txtTim.SelectAll();
            }
        }

        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            if (!CKiemTra.IsNull(txtTim, "Nhập nội dung cần tìm", traCuuErr))
            {
                btnTim.Enabled = true;
            }
            else
            {
                btnTim.Enabled = false;
            }

        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (!CKiemTra.IsNull(txtTim, "Nhập nội dung cần tìm", traCuuErr))
            {
                List<CDauSach> lst = new List<CDauSach>();
                string key = CXuLiChuoi.VietHoaDauMoiChu(txtTim.Text);
                if (rdbTenSach.Checked)
                {
                    lst = bllDauSach.LoadDauSachByColumnsName("TenDauSach", key);
                   /*
                    foreach (CDauSach ds in dataDauSach)
                    {
                        if (ds.TenDauSach.ToUpper().Contains(key))
                        {
                            lst.Add(ds);
                        }
                    }
                    * */
                }
                else if (rdbTheLoai.Checked)
                {
                    lst = bllDauSach.LoadDauSachByColumnsName("TheLoai", key); 
                    /*
                    foreach (CDauSach ds in dataDauSach)
                    {
                        if (ds.TheLoai.ToUpper().Contains(key))
                        {
                            lst.Add(ds);
                        }
                    }
                     * */
                }
                else if (rdbTacGia.Checked)
                {
                    lst = bllDauSach.LoadDauSachByColumnsName("TacGia", key); 
                    /*
                    foreach (CDauSach ds in dataDauSach)
                    {
                        if (ds.TacGia.ToUpper().Contains(key))
                        {
                            lst.Add(ds);
                        }
                    }
                     * */
                }
                else//nha xuat ban
                {
                    lst = bllDauSach.LoadDauSachByColumnsName("NhaXB", key);
                    /*
                    foreach (CDauSach ds in dataDauSach)
                    {
                        if (ds.NhaXB.ToUpper().Contains(key))
                        {
                            lst.Add(ds);
                        }
                    }
                     * */
                }

                /*
                if (lst.Count > 0)
                {
                    dgvKetQua.DataSource = lst;
                    dgvKetQua.Visible = true;
                }
                 * */
                if (lst != null)
                {
                    dgvKetQua.DataSource = lst;
                    dgvKetQua.Visible = true;
                }
                else
                {
                    MessageBox.Show("Không tìm thấy");
                }

                txtTim.Focus();
                txtTim.SelectAll();
            }
        }

        private void txtTim_KeyDown(object sender, KeyEventArgs e)
        {
            if (!CKiemTra.IsNull(txtTim, "Nhập nội dung tìm kiếm", traCuuErr))
            {
                if (e.KeyCode == Keys.Enter)
                {
                    btnTim_Click(sender, e);
                }
            }
        }
    }
}
